class Step extends egret.Sprite {
	public constructor() {
		super();
		this.addEventListener(egret.Event.ADDED_TO_STAGE, this.addToStage, this);
	}

	// 对象池：	用于台阶对象反复使用
	public pool = [];

	// 对象池：	用于得分点的Bitmap对象，因为用量较大，所以只有这个对象使用
	public Bitmap_pool = [];

	//	获取对象的方法
	//	参数：对象的种类，对象的类型（如果没有的话，默认Sprite）参数
	getObject(str, type = "Sprite", prama) {
		// console.log(this.pool[str])
		if (this.pool[str].length) {
			console.log("用了pool")
			return this.pool[str].shift();
		}
		else if (type == "Bitmap") {
			return new egret.Bitmap();
		}
		else if (type == "Sprite") {
			console.log("new了一个")
			return new mySprite(prama);
		}
	}


	// 基础的偏移量
	public moveX;
	public num;
	// 是否有石头
	public stone;

	public points = [{
		type: "face",
		src: "face_png",
		score: "+5"
	}, {
		type: "table",
		src: "table_png",
		score: "+6"
	}, {
		type: "desk",
		src: "desk_png",
		score: "+4"
	}, {
		type: "sofa",
		src: "sofa_png",
		score: "+3"
	}, {
		type: "star",
		src: "star_png",
		score: "+8"
	}]

	addToStage() {

		this.addStep({ x: 500, y: 900 }, true);
		this.addStep({ x: 1600, y: 700 }, true);
		this.addStep({ x: 2500, y: 500 }, true);
		this.moveX = 2500;

		this.stepMove();
	}
	// 添加台阶
	addStep(prama, NewObject) {

		// 石头对象
		var stone = null;
		// 是否有石头
		var isStone = true;

		// 是否有得分点
		var isPoint = true;


		// 台阶容器,台阶图片
		var sp, strick;

		if (NewObject) {

			sp = new mySprite({ "stone": stone });
			// 台阶图片
			strick = new egret.Bitmap();
			strick.texture = RES.getRes("Mc_pedal1_png");

			sp.addChild(strick);


		} else {
			sp = this.pool.shift();
			strick = sp.$children[0];
		}

		sp.x = prama.x;
		sp.y = prama.y;

		if (isStone) {

			//石头,随机x位置
			if (NewObject) {

				stone = new egret.Bitmap();
				stone.texture = RES.getRes("rock_png");
				stone.x = strick.width * 0.2 + Math.random() * strick.width * 0.6;
				stone.y = -stone.height;
				sp.prama.stone = stone;
				sp.addChild(stone);

			} else {
				sp.prama.stone.x = strick.width*0.2 + Math.random() * strick.width * 0.6 ;
				// sp.prama.stone.x = strick.width * 0.9;
			}
		}

		if (isPoint && ( !sp.prama.point || !sp.prama.point.length ) ) {
			let point;
			// 得分点个数
			let num = Math.ceil(Math.random() * 3);

			for (var k = 0; k < num; k++) {

				if (this.Bitmap_pool.length) {
					console.log(this.Bitmap_pool);
					point = this.Bitmap_pool.shift();
					console.log("point",point)
				} else {
					point = new egret.Sprite();
					point.image = new egret.Bitmap();
					point.addChild(point.image);
					point.score = new addScore();
					point.addChild(point.score);
				}

				// 随机图片
				let randomBitmap = Math.floor(Math.random()* 5);
				console.log(this.points[randomBitmap].src)
				point.image.texture = RES.getRes(this.points[randomBitmap].src);
				

				point.x = strick.width * 0.2 + Math.random() * strick.width * 0.6;
				point.y = -point.height - Math.random() * 300;
				point.width = point.image.width;
				point.height = point.image.height;

				point.flag = true;

				point.image.alpha = 1;
				point.image.scaleX = 1;
				point.image.scaleY = 1;
				point.image.$visible = true;
				point.image.anchorOffsetX = point.width * .5;
				point.image.anchorOffsetY = point.height * .5;

				
				// console.log("这个Bitmap",point);
				if (!sp.prama.point) {
					sp.prama.point = [];
				}
				// 添加到台阶里的得分数组
				sp.prama.point.push(point);
				

				point.score.x = -.5 * point.score.width;
				point.score.y = 0;
				point.score.$alpha = 0;
				point.score.texting.text = this.points[randomBitmap].score;
				// point.addChild(score);



				sp.addChild(point);
			}


		}



		this.addChild(sp);


	}
	// 台阶移动和判断超出移除 
	stepMove() {
		this.addEventListener(egret.Event.ENTER_FRAME, function () {
			// 容器移动速度
			this.x -= 20;

			let NewObject = true;

			if (Math.abs(this.x) % 900 == 0) {
				if (this.pool.length >= 3) {
					NewObject = false;
				}
				this.addStep({ x: (this.moveX += 900), y: (900 - Math.random() * 500) }, NewObject);
				// this.addStep({ x: (this.moveX += 2500), y: (900) }, NewObject);
			}

			// 第一个台阶超出时移除
			if (this.x + (this.$children[0].x + this.$children[0].width) <= 0) {

				this.pool.push(this.$children[0]);
				//
				this.Bitmap_pool.push(...this.$children[0].prama.point);
				this.$children[0].prama.point = []; 
				console.log( this.Bitmap_pool )
				this.removeChildAt(0);
			}
		}, this)
	}




}